package com.shop.cloud.controller.transaction;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.DistributionMsg;
import com.shop.cloud.domain.hongfu.InviteConfig;
import com.shop.cloud.domain.hongfu.TransactionLog;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.service.hongfu.IDistributionMsgService;
import com.shop.cloud.service.hongfu.IInviteConfigService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 流水控制
 * @author Yan Weiwei
 *
 */
@RestController
@RequestMapping(value = "transaction")
public class TransactionController extends BaseController {

	@Autowired
	private IDistributionMsgService distributionMsgService;
	
	@Autowired
	private IInviteConfigService inviteConfigService;
	
	@ApiOperation(value = "获取邀请有礼管理列表", notes = "获取邀请有礼管理列表")
	@RequestMapping(value = "/transactionList", method = RequestMethod.POST)
	@ResponseBody
	public Response transactionList(@CurrentUserId Long userId, @RequestBody DistributionMsg distributionMsg){
		
		//搜索条件处理
		if(null != distributionMsg.getLogType()){
			int logType = distributionMsg.getLogType();
			TransactionLog transactionLog = new TransactionLog();
			transactionLog.setType(logType);
			distributionMsg.setTransactionLog(transactionLog);
		}
		if(null != distributionMsg.getUserType()){
			int userType = distributionMsg.getUserType();
			if(userType == 0){
				//邀请人
				if(null != distributionMsg.getSearchUserID()){
					distributionMsg.setToUser(distributionMsg.getSearchUserID());
				}
				if(null != distributionMsg.getSearchUserName() && StringUtils.isNotEmpty(distributionMsg.getSearchUserName())){
					distributionMsg.setToUserName(distributionMsg.getSearchUserName());
				}
			}
			if(userType == 1){
				//邀请对象
				if(null != distributionMsg.getSearchUserID()){
					distributionMsg.setFromUser(distributionMsg.getSearchUserID());
				}
				if(null != distributionMsg.getSearchUserName() && StringUtils.isNotEmpty(distributionMsg.getSearchUserName())){
					distributionMsg.setFromUserName(distributionMsg.getSearchUserName());
				}
			}
		}
		
		distributionMsg.setStatus(0);
		distributionMsg.setOrderBy("DISTRIBUTION_MSG.create_time DESC");
		Page<DistributionMsg> data = distributionMsgService.getPage(distributionMsg);
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "获取邀请有礼说明", notes = "获取邀请有礼说明")
	@RequestMapping(value = "/getInviteConfig", method = RequestMethod.POST)
	@ResponseBody
	public Response getInviteConfig(@CurrentUserId Long userId){
		
		InviteConfig inviteConfig = inviteConfigService.get(new Long(1));
		
		return ResponseUtil.ok(inviteConfig);
	}
	
	@ApiOperation(value = "修改邀请有礼说明", notes = "修改邀请有礼说明")
	@RequestMapping(value = "/modifyInviteConfig", method = RequestMethod.POST)
	@ResponseBody
	public Response modifyInviteConfig(@CurrentUserId Long userId, @RequestBody InviteConfig inviteConfig){
		
		inviteConfig.setId(new Long(1));
		int data = inviteConfigService.update(inviteConfig);
		
		return ResponseUtil.ok(data);
	}
}
